package com.company.cloud.user.modules.app.controller;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.user.constant.UserMsgSendTypeEnum;
import com.company.cloud.user.modules.app.controller.req.UserLoginByMobile2CodeParam;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.app.service.UserRegisterService;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.bus.producer.UserProducer;
import com.company.cloud.user.modules.cache.service.UserPermissionCacheService;
import com.company.common.server.config.cloud.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * (手机号+验证码)登录
 */
@Log4j2
@RestController
@RequestMapping("/app/user/login")
@Api(tags = "UC_用户(手机号+验证码)登录")
public class UserLogin2MobileCodeController {

    @Autowired
    private UserCompanyService userCompanyService;

    @Autowired
    private UserCodeService userCodeService;

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserPermissionCacheService userPermissionCacheService;

    @Autowired
    private ProjectConfig projectConfig;

    @Autowired
    private UserProducer userProducer;

    @Autowired
    private UserRegisterService userRegisterService;

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/loginByMobile2Code")
    @ApiOperation(value = "(手机号+验证码)登录", response = LoginUserInfo.class)
    public BaseResult loginByMobile2Code(@RequestBody UserLoginByMobile2CodeParam params) {
        ValidatorUtils.validate(params);
        String companyNo = userCompanyService.companyCheck(AuthContextUtils.getCompanyNo());
        userCodeService.validate(companyNo, UserMsgSendTypeEnum.Login_VCode, params.getMobile(), params.getVcode(), true);
        UserInfo userInfo = userLoginService.getUserByMobile(companyNo, params.getMobile());
        LoginUserInfo data = userLoginService.login(userInfo);
        userPermissionCacheService.loadPermissionRoleCache(data.getCompanyNo());
        userProducer.userLogin("手机号验证码登录", params, data);
        return BaseResult.ok(data);
    }

    @PostMapping("/loginByMobile2CodePossWord")
    @ApiOperation(value = "(手机号+验证码)修改密码", response = LoginUserInfo.class)
    public BaseResult loginByMobile2CodePossWord(@RequestBody UserLoginByMobile2CodeParam params) {
        LoginSimpleUserInfo loginSimpleUserInfo = AuthContextUtils.getUser();
        UserInfo userInfo = userInfoService.getById(loginSimpleUserInfo.getId());
        if (!Objects.equals(userInfo.getMobile(), params.getMobile())) {
            throw new BusinessException("-6","当前手机号与原手机号不一致！");
        }
        ValidatorUtils.validate(params);
        String companyNo = userCompanyService.companyCheck(AuthContextUtils.getCompanyNo());

        /**
         * 验证登录验证码
         */
        userCodeService.validate(companyNo, UserMsgSendTypeEnum.Login_VCode, params.getMobile(), params.getVcode(), true);

        /**
         * 修改密码
         */
        userInfoService.modifyPassword(userInfo, params);

        /**
         * 退出用户登录
         */
        userPermissionCacheService.logoutUser(loginSimpleUserInfo);

        return BaseResult.ok();
    }
    @PostMapping("/loginByMobile2CodeCancel")
    @ApiOperation(value = "(手机号+验证码)注销账户", response = LoginUserInfo.class)
    public BaseResult loginByMobile2CodeCancel(@RequestBody UserLoginByMobile2CodeParam params) {
        LoginSimpleUserInfo loginSimpleUserInfo = AuthContextUtils.getUser();
        UserInfo userInfo = userInfoService.getById(loginSimpleUserInfo.getId());
        if (!Objects.equals(userInfo.getMobile(), params.getMobile())) {
            throw new BusinessException("-6","当前手机号与原手机号不一致！");
        }
        ValidatorUtils.validate(params);
        String companyNo = userCompanyService.companyCheck(AuthContextUtils.getCompanyNo());

        /**
         * 验证登录验证码
         */
        userCodeService.validate(companyNo, UserMsgSendTypeEnum.Login_VCode, params.getMobile(), params.getVcode(), true);

        /**
         * 注销
         */
        userInfoService.modifyCancel(userInfo, params);

        /**
         * 退出用户登录
         */
        userPermissionCacheService.logoutUser(loginSimpleUserInfo);

        return BaseResult.ok();
    }

}
